package io.renren.modules.jsf.controller;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.jsf.entity.QianDaoEntity;
import io.renren.modules.jsf.service.QianDaoService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import static io.renren.common.utils.ShiroUtils.getUserId;


/**
 * @author chen
 * @email sun@gmail.com
 * @date 2022-04-10 00:35:10
 */
@Slf4j
@RestController
@RequestMapping("jsf/qiandao")
public class QianDaoController {
    @Autowired
    private QianDaoService qianDaoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("jsf:qiandao:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = qianDaoService.queryPage(params);
        if (getUserId() != 1) {
            List<QianDaoEntity> list = (List<QianDaoEntity>) page.getList();
            List<QianDaoEntity> data = list.stream().filter(t -> {
                return t.getUserId().equals(getUserId());
            }).collect(Collectors.toList());
            page.setList(data);
            return R.ok().put("page", page);
        } else {
            return R.ok().put("page", page);
        }
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{userId}")
    @RequiresPermissions("jsf:qiandao:info")
    public R info(@PathVariable("userId") Integer userId) {
        QianDaoEntity qianDao = qianDaoService.getById(userId);
        return R.ok().put("qianDao", qianDao);
    }

    @Autowired
    SysUserService userService;

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("jsf:qiandao:save")
    public R save(@RequestBody QianDaoEntity qianDao) {
        SysUserEntity userEntity = userService.selectOneByName(qianDao.getUserName());
        if (userEntity == null) {
            return R.error("用户不存在！");
        }
        if (getUserId() != 1) {
            if (!userEntity.getUserId().equals(getUserId())) {
                return R.error("当前用户非本人！");
            }
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = sdf.format(new Date());
        log.info("签到时间,{}", date.substring(0, 10));
        QianDaoEntity qianDaoEntity = qianDaoService.selectByTimeAndName(date.substring(0, 10), userEntity.getUsername());
        if (qianDaoEntity != null) {
            log.info("签到时间,{}", qianDaoEntity.getCreateTime().substring(0, 10));
            if (qianDaoEntity.getCreateTime().substring(0, 10).equals(date.substring(0, 10))) {
                return R.error("已签到！请勿重复提交");
            }
        }
        //入库签到
        qianDao.setUserId(userEntity.getUserId());
        qianDao.setUserName(userEntity.getUsername());
        qianDao.setCreateTime(date);
        qianDaoService.save(qianDao);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("jsf:qiandao:update")
    public R update(@RequestBody QianDaoEntity qianDao) {
        qianDaoService.updateById(qianDao);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("jsf:qiandao:delete")
    public R delete(@RequestBody Integer[] userIds) {
        qianDaoService.removeByIds(Arrays.asList(userIds));

        return R.ok();
    }

}
